package org.malred.hotelsystem.adapter;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.malred.hotelsystem.R;
import org.malred.hotelsystem.activities.BaseActivity;
import org.malred.hotelsystem.dao.HotelDao;
import org.malred.hotelsystem.dao.OrderDao;
import org.malred.hotelsystem.dao.UserDao;
import org.malred.hotelsystem.model.entity.HotelEntity;
import org.malred.hotelsystem.model.entity.OrderEntity;
import org.malred.hotelsystem.model.entity.UserEntity;
import org.malred.hotelsystem.utils.DBUtil;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;

public class OrderRecycleAdapter extends RecyclerView.Adapter<OrderRecycleAdapter.Holder> {
    private static final String TAG = "OrderRecycleAdapter";
    List<OrderEntity> items;
    Context context;
    // 它的作用类似于 findViewById()
    LayoutInflater inflater;

    public OrderRecycleAdapter(Context context, List<OrderEntity> items) {
        this.context = context;
        this.items = items;
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 每个item的xml布局
        View view = inflater.inflate(R.layout.manage_list_item_order_layout, parent, false);

        // 可以在这给holder(单个list项)注册点击
        Holder holder = new Holder(view);
        return holder;
    }

    // 给holder里的控件赋值
    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        OrderEntity orderEntity = items.get(position);
        SQLiteDatabase db = new DBUtil(context).getReadableDatabase();

        Log.i(TAG, "onBindViewHolder: " + orderEntity.toString());

        HotelEntity hotelEntity = HotelDao.getById(db, orderEntity.getHid());
        holder.tvTitle.setText(hotelEntity.getTitle());

        UserEntity userEntity = UserDao.getById(db, orderEntity.getUid());
        holder.tvUsername.setText(userEntity.getName());

        holder.tvDate.setText(orderEntity.getoDate());

        holder.oid = orderEntity.getId();
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tvTitle;
        TextView tvDate;
        TextView tvUsername;
        String oid;

        public Holder(@NonNull View itemView) {
            super(itemView);

            this.tvDate = itemView.findViewById(R.id.tv_date);
            this.tvUsername = itemView.findViewById(R.id.tv_username);
            this.tvTitle = itemView.findViewById(R.id.tv_title);

            tvDate.setOnClickListener(this);
            tvTitle.setOnClickListener(this);
            tvUsername.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {
            // 显示对话框, 入住|退订
            BaseActivity activity = (BaseActivity) context;
            OrderEntity orderEntity = OrderDao.getById(activity.getDb(), oid);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            activity.alertClick("修改状态", "修改状态为?", "入住", "退订",
                    // 入住
                    (dialog, which) -> {
                        Log.i(TAG, "onClick: 入住");
                        orderEntity.setoDate(sdf.format(new Date()));
                        orderEntity.setStatus("1");
                        OrderDao.upt(activity.getWritableDb(), orderEntity);
                        dialog.cancel();
                        activity.showToastSync("修改成功");
                    },
                    // 退订
                    (dialog, which) -> {
                        Log.i(TAG, "onClick: 退订");
                        orderEntity.setoDate(sdf.format(new Date()));
                        orderEntity.setStatus("2");
                        OrderDao.upt(activity.getWritableDb(), orderEntity);
                        dialog.cancel();
                        activity.showToastSync("修改成功");
                    }
            );
        }
    }
}

